//
//  DaysConditionsView.m
//  MonitoringNetwork
//
//  Created by admin on 2018/1/3.
//  Copyright © 2018年 wtmcxlm. All rights reserved.
//

#import "DaysConditionsView.h"
#import "NSString+File.h"

#import "DaysHistogramViewCell.h"
#import "CusTableViewCell.h"
#import "DaysOperationTotalCell.h"

#import "ThreeLinesCell.h"
#import "OperationPiechartCell.h"

#import "ChartTableModel.h"
#import "DaysOperationPiechartlCell.h"




static NSString *DayConCusTableViewCellID = @"DayConCusTableViewCellID";

@interface DaysConditionsView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *daysConditionsTableView;

@property (nonatomic,strong)NSArray *maxAverageSpeed;
@property (nonatomic,strong)NSArray *maxSpeed;
@property (nonatomic,strong)NSDictionary *mileage;
@property (nonatomic,strong)NSArray *mileageRankDesc;
@property (nonatomic,strong)NSArray *mileageStatis;
@property (nonatomic,strong)NSArray *runtimeRankDesc;
@property (nonatomic,assign)BOOL allowLoad;
@property (nonatomic,strong)LoadNetView *loadNetView;
@end
//static NSString *DaysAlermHistogramViewCellID = @"DaysAlermHistogramViewCellID";
//static NSString *DaysAlermCusTableViewCellID = @"DaysAlermCusTableViewCellID";

@implementation DaysConditionsView
- (LoadNetView *)loadNetView
{
    if (!_loadNetView) {
        _loadNetView = [LoadNetView new];
        [self addSubview:_loadNetView];
        [_loadNetView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.mas_equalTo(self);
        }];
        _loadNetView.hidden = NO;
    }
    return _loadNetView;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _allowLoad = NO;
        _needRefresh = YES;
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI
{
    _daysConditionsTableView = [UITableView new];
    _daysConditionsTableView.dataSource = self;
    _daysConditionsTableView.delegate = self;
    _daysConditionsTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    _daysConditionsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _daysConditionsTableView.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
    [_daysConditionsTableView registerClass:[CusTableViewCell class] forCellReuseIdentifier:DayConCusTableViewCellID];
    [self addSubview:_daysConditionsTableView];
    [_daysConditionsTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(self);
        make.bottom.mas_equalTo(self.mas_bottom).offset(-TabBarHeight-10);
    }];
    
}

- (void)loadNewData
{
    [self.loadNetView start];
    if (!_param) {
        _param = @{
                   @"startDate":[NSString getDateString3],
                   @"endDate":[NSString getCurrentDate2]
                   };
    }
    [[NetWorkTool sharedNetworkTool] requestWithUrlString:kURL_travelStatisByDays method:@"POST" parameters:_param progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(id  _Nullable responseObject) {
        [self.loadNetView endAnimationWithResult:LoadingViewResultTypeSuccess];
        NSLog(@"%@",responseObject);
        if ([responseObject[@"state"] integerValue] == 0) {
            NSDictionary *dict = responseObject[@"obj"];
            self.maxAverageSpeed = dict[@"maxAverageSpeed"];
            self.maxSpeed = dict[@"maxSpeed"];
            self.mileage = dict[@"mileage"];
            self.mileageRankDesc = dict[@"mileageRankDesc"];
            self.mileageStatis = dict[@"mileageStatis"];
            self.runtimeRankDesc = dict[@"runtimeRankDesc"];
            _allowLoad = YES;

        }
        [_daysConditionsTableView.mj_header endRefreshing];
        [_daysConditionsTableView reloadData];
    } failure:^(NSError * _Nonnull error) {
        [self.loadNetView endAnimationWithResult:LoadingViewResultTypeError];
        [_daysConditionsTableView.mj_header endRefreshing];
        NSLog(@"%@",error);
    }];
     [_daysConditionsTableView.mj_header endRefreshing];
}
- (void)loadDataWithParam:(NSDictionary *)param
{
    
    if (_needRefresh) {
        if (!_param) {
            _param = @{
                       @"startDate":[NSString getDateString3],
                       @"endDate":[NSString getCurrentDate2]
                       };
        }
        [self.loadNetView start];
        [[NetWorkTool sharedNetworkTool] requestWithUrlString:kURL_travelStatisByDays method:@"POST" parameters:_param progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [self.loadNetView endAnimationWithResult:LoadingViewResultTypeSuccess];
            NSLog(@"%@",responseObject);
            if ([responseObject[@"state"] integerValue] == 0) {
                NSDictionary *dict = responseObject[@"obj"];
                self.maxAverageSpeed = dict[@"maxAverageSpeed"];
                self.maxSpeed = dict[@"maxSpeed"];
                self.mileage = dict[@"mileage"];
                self.mileageRankDesc = dict[@"mileageRankDesc"];
                self.mileageStatis = dict[@"mileageStatis"];
                self.runtimeRankDesc = dict[@"runtimeRankDesc"];
                _allowLoad = YES;
                _needRefresh = NO;
            }
            [_daysConditionsTableView.mj_header endRefreshing];
            [_daysConditionsTableView reloadData];
        } failure:^(NSError * _Nonnull error) {
            [self.loadNetView endAnimationWithResult:LoadingViewResultTypeError];
            [_daysConditionsTableView.mj_header endRefreshing];
            NSLog(@"%@",error);
        }];
    }
    
}
- (void)loadDataBackGroundWithParam:(NSDictionary *)param
{
    
    if (_needRefresh) {
        if (!_param) {
            _param = @{
                       @"startDate":[NSString getDateString3],
                       @"endDate":[NSString getCurrentDate2]
                       };
        }
        [self.loadNetView start];
        [[NetWorkTool sharedNetworkTool] requestWithUrlString:kURL_travelStatisByDays method:@"POST" parameters:_param progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [self.loadNetView endAnimationWithResult:LoadingViewResultTypeSuccess];
            NSLog(@"%@",responseObject);
            if ([responseObject[@"state"] integerValue] == 0) {
                NSDictionary *dict = responseObject[@"obj"];
                self.maxAverageSpeed = dict[@"maxAverageSpeed"];
                self.maxSpeed = dict[@"maxSpeed"];
                self.mileage = dict[@"mileage"];
                self.mileageRankDesc = dict[@"mileageRankDesc"];
                self.mileageStatis = dict[@"mileageStatis"];
                self.runtimeRankDesc = dict[@"runtimeRankDesc"];
                _allowLoad = YES;
                _needRefresh = NO;
            }
            [_daysConditionsTableView.mj_header endRefreshing];
            [_daysConditionsTableView reloadData];
        } failure:^(NSError * _Nonnull error) {
            [self.loadNetView endAnimationWithResult:LoadingViewResultTypeError];
            [_daysConditionsTableView.mj_header endRefreshing];
            NSLog(@"%@",error);
        }];
    }
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_allowLoad) {
        return 8;
    }else{
        return 0;
    }

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:
        {
            DaysOperationTotalCell *totalcell = [DaysOperationTotalCell new];
            if (self.mileage) {
                totalcell.total = [NSString stringWithFormat:@"%@",self.mileage[@"allCarNum"]];
            }
            totalcell.selectionStyle = UITableViewCellSelectionStyleNone;
            return totalcell;
        }
            break;
        case 1:
        {
            ThreeLinesCell *totalMileagecell = [ThreeLinesCell new];
            if (self.mileage) {
                totalMileagecell.label1.text = @"行驶里程总数(km)";
                totalMileagecell.label2.text = [NSString separatedDigitStringWithStr:[NSString stringWithFormat:@"%.2lf",[self.mileage[@"totalMileage"] doubleValue]]];
                totalMileagecell.label3.text = @"平均每日行驶里程(km)";
                totalMileagecell.label4.text = [NSString separatedDigitStringWithStr:[NSString stringWithFormat:@"%.2lf",[self.mileage[@"avgDayMileage"] doubleValue]]];
                totalMileagecell.label5.text = @"平均每日每车行驶里程(km)";
                totalMileagecell.label6.text = [NSString separatedDigitStringWithStr:[NSString stringWithFormat:@"%.2lf",[self.mileage[@"avgDayAndCarMileage"] doubleValue]]] ;
            }
            totalMileagecell.selectionStyle = UITableViewCellSelectionStyleNone;
            return totalMileagecell;
        }
            break;
        case 2:
        {
            ThreeLinesCell *totalHoursCell = [ThreeLinesCell new];
            if (self.mileage) {
                totalHoursCell.label1.text = @"行驶时长(h)";
                totalHoursCell.label2.text = [NSString separatedDigitStringWithStr:[NSString stringWithFormat:@"%.2lf",[self.mileage[@"totalRunTime"] doubleValue]]];
                totalHoursCell.label3.text = @"平均每日行驶时长(h)";
                totalHoursCell.label4.text = [NSString separatedDigitStringWithStr:[NSString stringWithFormat:@"%.2lf",[self.mileage[@"avgDayRunTime"] doubleValue]]];
                totalHoursCell.label5.text = @"平均每日每车行驶时长(h)";
                totalHoursCell.label6.text = [NSString separatedDigitStringWithStr:[NSString stringWithFormat:@"%.2lf",[self.mileage[@"avgDayAndCarTime"] doubleValue]]];
            }
            totalHoursCell.selectionStyle = UITableViewCellSelectionStyleNone;
            return totalHoursCell;
        }
            break;
        case 3:
        {
            DaysOperationPiechartlCell *daysPieCell = [DaysOperationPiechartlCell new];
            daysPieCell.title = @"行驶里程分布";
            if (self.mileageStatis) {
                [daysPieCell laodUIWithDataArr:self.mileageStatis];
            }
            daysPieCell.selectionStyle = UITableViewCellSelectionStyleNone;
            return daysPieCell;
        }
            break;
       
        case 4:
        {
            CusTableViewCell *totalmilecell = [tableView dequeueReusableCellWithIdentifier:DayConCusTableViewCellID];
            totalmilecell.title = @"行驶里程排行前5名";
            totalmilecell.leftString = @"排名";
            totalmilecell.rightString = @"里程数 (km)";
            totalmilecell.centerString = @"车牌号";
            if(self.mileageRankDesc){
                NSMutableArray *tempArr = [NSMutableArray array];
                for (int i=0; i<self.mileageRankDesc.count; i++) {
                    NSDictionary *dic = self.mileageRankDesc[i];
                    ChartTableModel *model = [ChartTableModel new];
                    model.leftString = [dic[@"runk"] stringValue];
                    model.centerString = dic[@"plateNum"] ;
                    model.rightString = [NSString stringWithFormat:@"%.2lf",[dic[@"totalMileage"] doubleValue]];
                    [tempArr addObject:model];
                }
                [totalmilecell laodTableWithDataArr:tempArr.copy];
            }
            totalmilecell.selectionStyle = UITableViewCellSelectionStyleNone;
            return totalmilecell;
        }
            break;
        case 5:
        {
            CusTableViewCell *totalhourcell = [tableView dequeueReusableCellWithIdentifier:DayConCusTableViewCellID];
            totalhourcell.title = @"行驶时长排行前5名";
            totalhourcell.leftString = @"排名";
            totalhourcell.rightString = @"时长 (h)";
            totalhourcell.centerString = @"车牌号";
            if(self.runtimeRankDesc){
                NSMutableArray *tempArr = [NSMutableArray array];
                for (int i=0; i<self.runtimeRankDesc.count; i++) {
                    NSDictionary *dic = self.runtimeRankDesc[i];
                    ChartTableModel *model = [ChartTableModel new];
                    model.leftString = [dic[@"runk"] stringValue];
                    model.centerString = dic[@"plateNum"];
                    model.rightString = [NSString stringWithFormat:@"%.2lf",[dic[@"totalRunTime"] doubleValue]];
                    [tempArr addObject:model];
                }
                [totalhourcell laodTableWithDataArr:tempArr.copy];
            }
            totalhourcell.selectionStyle = UITableViewCellSelectionStyleNone;
            return totalhourcell;
        }
            break;
        case 6:
        {
            CusTableViewCell *aveSpeedcell = [tableView dequeueReusableCellWithIdentifier:DayConCusTableViewCellID];
            aveSpeedcell.title = @"平均速度排行前5名";
            aveSpeedcell.leftString = @"排名";
            aveSpeedcell.rightString = @"速度 (km/h)";
            aveSpeedcell.centerString = @"车牌号";
            if(self.maxAverageSpeed){
                NSMutableArray *tempArr = [NSMutableArray array];
                for (int i=0; i<self.maxAverageSpeed.count; i++) {
                    NSDictionary *dic = self.maxAverageSpeed[i];
                    ChartTableModel *model = [ChartTableModel new];
                    model.leftString = [NSString stringWithFormat:@"%@",dic[@"runk"]];
                    model.centerString = dic[@"plateNum"];
                    model.rightString = [NSString stringWithFormat:@"%.2lf",[dic[@"averagespeed"] doubleValue]];
                    [tempArr addObject:model];
                }
                [aveSpeedcell laodTableWithDataArr:tempArr.copy];
            }
            aveSpeedcell.selectionStyle = UITableViewCellSelectionStyleNone;
            return aveSpeedcell;
        }
            break;
        case 7:
        {
            CusTableViewCell *maxSpeedcell = [tableView dequeueReusableCellWithIdentifier:DayConCusTableViewCellID];
            maxSpeedcell.title = @"最高速度排行前5名";
            maxSpeedcell.title = @"最高速度排行前5名";
            maxSpeedcell.leftString = @"排名";
            maxSpeedcell.rightString = @"速度 (km/h)";
            maxSpeedcell.centerString = @"车牌号";
            if(self.maxSpeed){
                NSMutableArray *tempArr = [NSMutableArray array];
                for (int i=0; i<self.maxSpeed.count; i++) {
                    NSDictionary *dic = self.maxSpeed[i];
                    ChartTableModel *model = [ChartTableModel new];
                    model.leftString = [NSString stringWithFormat:@"%@",dic[@"runk"]];
                    model.centerString = dic[@"plateNum"];
                    model.rightString = [NSString stringWithFormat:@"%.2lf",[dic[@"speed"] doubleValue]];
                    [tempArr addObject:model];
                }
                [maxSpeedcell laodTableWithDataArr:tempArr.copy];
            }
            maxSpeedcell.selectionStyle = UITableViewCellSelectionStyleNone;
            return maxSpeedcell;
        }
            break;
        default:
            return nil;
            break;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:
            return 94;
            break;
        case 1:
            return 140;
            break;
        case 2:
            return 140;
            break;
        case 3:
            return 299;
            break;
        case 4:
            return 355;
            break;
        case 5:
            return 355;
            break;
        case 6:
            return 355;
            break;
        case 7:
            return 355;
            break;
        default:
            return 355;
            break;
    }
}

@end
